package com.fh.service.airsys.registeronlinetran;

import java.util.List;

import javax.annotation.Resource;

import com.fh.dto.TranRequest;
import com.fh.entity.HttpResult;
import com.fh.entity.system.User;
import com.fh.exception.ServiceException;
import com.fh.service.airsys.airlineemployee.AirlineEmployeeService;
import com.fh.service.transaction.OnLineTranService;
import com.fh.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.util.PageData;


@Service("registeronlinetranService")
public class RegisteronLineTranService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;
	@Autowired
	AirlineEmployeeService airlineEmployeeService;
	@Autowired
	OnLineTranService onLineTranService;
	
	/*
	* 新增
	*/
	public void save(PageData pd)throws Exception{
		pd.put("EI_EMINFO",pd.getString("EI_CODE"));
		PageData empData = airlineEmployeeService.findById(pd);
		pd.put("TD_TIME",pd.getString("TD_TIME").replaceAll("-","").replaceAll(" ","").replaceAll(":",""));
		pd.put("RL_TIME",pd.getString("RL_TIME").replaceAll("-","").replaceAll(" ","").replaceAll(":",""));
		pd.put("RL_AMOUNT",Double.valueOf(pd.getString("RL_AMOUNT"))*100);
		pd.put("SA_ACC",empData.get("SA_ACC"));
		pd.put("RL_STATE",1);
		dao.save("RegisteronLineTranMapper.save", pd);
	}
	
	/*
	* 删除
	*/
	public void delete(PageData pd)throws Exception{
		dao.delete("RegisteronLineTranMapper.delete", pd);
	}
	
	/*
	* 修改
	*/
	public void edit(PageData pd)throws Exception{
		dao.update("RegisteronLineTranMapper.edit", pd);
	}
	
	/*
	*列表
	*/
	public List<PageData> list(Page page)throws Exception{
		List<PageData> list = (List<PageData>)dao.findForList("RegisteronLineTranMapper.datalistPage", page);
		for (PageData data:list){
 			data.put("TD_TIME", DateUtil.getYYYY_MM_DD_HH_MM_SS(data.getString("TD_TIME")));
			data.put("RL_TIME", DateUtil.getYYYY_MM_DD_HH_MM_SS(data.getString("RL_TIME")));
			data.put("RL_FINAL_TIME", DateUtil.getYYYY_MM_DD_HH_MM_SS(data.getString("RL_FINAL_TIME")));
		}
		return list;
	}
	
	/*
	*列表(全部)
	*/
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RegisteronLineTranMapper.listAll", pd);
	}
	
	/*
	* 通过id获取数据
	*/
	public PageData findById(PageData pd)throws Exception{
		return (PageData)dao.findForObject("RegisteronLineTranMapper.findById", pd);
	}
	
	/*
	* 批量删除
	*/
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		dao.delete("RegisteronLineTranMapper.deleteAll", ArrayDATA_IDS);
	}

	public boolean checkRegisterTran(String[] ArrayDATA_IDS)throws Exception{
		PageData pd = new PageData();
		pd.put("idList",getId(ArrayDATA_IDS));
		List<PageData> list = listAll(pd);
		for (PageData pageData:list){
			Integer state = (Integer) pageData.get("RL_STATE");
			if(state != 1 || state != 5){
				return false;
			}
		}
		return true;
	}

	/**
	 * 取消补录交易--财务批量操作
	 * @param ArrayDATA_IDS
	 */
	public void cancelRegisterTran(String[] ArrayDATA_IDS, User user) throws Exception {
		PageData pd = new PageData();
		pd.put("RL_TYPE",2);
		pd.put("RL_STATE",3);
		pd.put("RL_CW_USERNAME",user.getUSER_ID());
		pd.put("RL_FINAL_TIME",DateUtil.getTime());
		pd.put("idList",getId(ArrayDATA_IDS));
		dao.update("RegisteronLineTranMapper.edit", pd);
	}

	/**
	 * 取消补录交易--财务批量操作
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void commitRegisterTran(String[] ArrayDATA_IDS, User user) throws Exception {
		PageData pd = new PageData();
		pd.put("RL_TYPE",2);
		pd.put("RL_STATE",2);
		pd.put("RL_CW_USERNAME",user.getUSER_ID());
		pd.put("RL_FINAL_TIME",DateUtil.getTime());
		pd.put("idList",getId(ArrayDATA_IDS));
		List<PageData> list = listAll(pd);
		if(list == null || list.size() ==0)
			throw new ServiceException(50101,"补录信息错误！");
		for (PageData pageData:list){
			TranRequest request = new TranRequest();
			request.setTranType("1014");
			request.setEmpId(pageData.getString("EI_CODE"));
			request.setOrgId(user.getAIRLINEINFO_ID());
			request.setRummeryId(pageData.getString("RI_CODE"));
			request.setTranAmt((Integer) pageData.get("RL_AMOUNT")*-1);
			request.setTson(pageData.getString("RL_CODE"));
			onLineTranService.tranExce(request);
		}
		dao.update("RegisteronLineTranMapper.edit", pd);
	}

	public String getId(String[] ArrayDATA_IDS){
		StringBuffer sb = new StringBuffer();
		for (String s:ArrayDATA_IDS){
			sb.append("'");
			sb.append(s);
			sb.append("'");
			sb.append(",");
		}
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
}

